<?php

namespace app\admin\controller;

use app\admin\controller\CommonController;
use think\Validate;
use think\Db;
use app\admin\model\Miaosha;
use app\admin\model\Item;
use app\admin\model\Order;

class MiaoshaController extends CommonController {
    /*
     * 秒杀列表
     * author xfwed 
     * date 2019.09.10
     */

    public function miaoshaList() {
        $perPage = input('param.perPage');
        $page = input('param.page');

        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;

        $miaoshaList = Miaosha::order('id desc')->paginate($perPage);
        foreach ($miaoshaList as $k => $v) {
            $orderdata = Db::name('order')->alias('o')->join('ord_pro p', 'p.ord_id = o.id', 'left')
                    ->where('p.type', '=', 5)
                    ->where('o.status', '=', 2)
                    ->where('p.pro_id', '=', $v['id'])
                    ->field('sum(p.number) number,sum(o.pay_money) pay_money')
                    ->find();
            $miaoshaList[$k]['orderNum'] = isset($orderdata['number']) ? $orderdata['number'] : 0;
            $miaoshaList[$k]['orderMoney'] = isset($orderdata['pay_money']) ? $orderdata['pay_money'] : 0;
        }
        return $this->ajaxReturn(0, '查询成功', $miaoshaList);
    }

    /*
     * 添加秒杀
     * author xfwed 
     * date 2019.09.10
     */

    public function addMiaosha() {
        $rule = [
            'name' => 'require',
            'start_time' => 'require',
            'num' => 'require',
            'price' => 'require',
            'itemId' => 'require',
        ];
        $msg = [
            'name.require' => '秒杀名称必须',
            'start_time.require' => '开始时间必须',
            'num.require' => '活动名额必须',
            'price.require' => '抢购价必须',
            'itemId.require' => '项目id必须',
        ];
        $all = input('param.');
        $validate = new Validate($rule, $msg);

        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }
        $itemId = $all['itemId']; //项目id
        $item_price = Item::where('id', '=', $itemId)->value('item_price');

        $data['name'] = $all['name'];
        $data['start_time'] = $all['start_time'];
        $data['item_id'] = $all['itemId'];
        $data['price'] = $all['price'];
        $data['item_price'] = $item_price;
        $data['number'] = $all['num'];
        $data['add_time'] = date('Y-m-d H:i:s');
        $insertId = Miaosha::insertGetId($data);
        if ($insertId) {
            return $this->ajaxReturn(0, '添加成功', null);
        } else {
            return $this->ajaxReturn(3, '添加失败', null);
        }
    }

    /*
     * 下架秒杀
     * author xfwed 
     * date 2019.09.10
     */

    public function delMiaosha() {
        $id = input('param.id');
        $data['is_del'] = 2;
        $data['updated_time'] = date('Y-m-d H:i:s');
        $add = Miaosha::where('id', '=', $id)->update($data);
        if ($add) {
            return $this->ajaxReturn(0, '下架成功', null);
        } else {
            return $this->ajaxReturn(3, '下架失败', null);
        }
    }

    /*
     * 秒杀详情
     * author xfwed 
     * date 2019.09.18
     */

    public function miaoshaInfo() {
        $id = input('param.id');
        $miaoshaRow = Miaosha::alias('m')->join('item i', 'm.item_id = i.id', 'left')
                ->where('m.id', '=', $id)
                ->field('m.*,i.item_name')
                ->find();


        $orderList = Db::name('order')->alias('o')
                ->join('ord_pro p', 'p.ord_id = o.id', 'left')
                ->where('p.type', '=', 5)
                ->where('o.status', '=', 2)
                ->where('p.pro_id', '=', $id)
                ->field('o.ctm_id,o.pay_time,o.pay_money')
                ->select();

        $newOrderNum = 0;
        $oldOrderNum = 0;
        $newOrderMoney = 0;
        $oldOrderMoney = 0;
        foreach ($orderList as $k => $v) {
            $orderRow = Order::where('ctm_id', '=', $v['ctm_id'])
                    ->where('status', '=', 2)
                    ->where('pay_time', '<', $v['pay_time'])
                    ->count();
            if ($orderRow > 0) {
                $oldOrderNum = $oldOrderNum + 1;
                $oldOrderMoney = $oldOrderMoney + $v['pay_money'];
            } else {
                $newOrderNum = $newOrderNum + 1;
                $newOrderMoney = $newOrderMoney + $v['pay_money'];
            }
        }
        $miaoshaRow['newOrderNum'] = $newOrderNum;
        $miaoshaRow['oldOrderNum'] = $oldOrderNum;
        $miaoshaRow['newOrderMoney'] = $newOrderMoney;
        $miaoshaRow['oldOrderMoney'] = $oldOrderMoney;

        return $this->ajaxReturn(0, '查询成功', $miaoshaRow);
    }

}
